package io.renren.modules.seller.controller;

import io.renren.common.utils.Constant;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.entity.SellerTemporaryEntity;
import io.renren.modules.seller.service.SellerService;
import io.renren.modules.seller.service.SellerTemporaryService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.shiro.ShiroUtils;
import jdk.nashorn.internal.runtime.ConsString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 商家信息暂存表-商家审核
 *
 * @author Mark  15373178086@163.com
 * @date 2020-02-20 12:56:19
 */
@Slf4j
@RestController
@RequestMapping("temporary/seller")
public class SellerTemporaryController {

    @Autowired
    private SellerTemporaryService sellerTemporaryService;
    @Autowired
    private SellerService sellerService;

    /**
     * 返回全部列表
     *
     * @return list
     */
    @RequestMapping("/findAll")
    public List<SellerTemporaryEntity> findAll() {
        return sellerTemporaryService.list();
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(String page, String rows) {
        return sellerTemporaryService.findPage(page, rows);
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody SellerTemporaryEntity seller, String page, String rows) {
        return sellerTemporaryService.findPage(seller, page, rows);
    }

    /**
     * 商家入驻
     *
     * @return Result
     */
    @RequestMapping("/settle")
    public Result settle(@RequestBody SellerTemporaryEntity seller) {
        // 入驻直接待审
        seller.setStatus(Constant.SellerStatus.one.value());
        seller.setCreateTime(new Date());
        seller.setMobile(seller.getLinkmanMobile());
        seller.setEmail(seller.getLinkmanEmail());

        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUser.setSalt(salt);
        sysUser.setPassword(ShiroUtils.sha256(sysUser.getPassword(), sysUser.getSalt()));
        sysUser.setEmail(seller.getEmail());
        sysUser.setMobile(seller.getMobile());
        sysUser.setUsername(seller.getUsername());

        sellerTemporaryService.settle(sysUser, seller);
        return Result.success();
    }

    /**
     * 0暂存-保存功能
     * 1待审核-提交功能
     * 2审核通过3审核未通过4关闭
     *
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody SellerTemporaryEntity seller) {
        try {
            String sellerId = ShiroUtils.getUserId().toString();
            seller.setSellerId(sellerId);
            SellerTemporaryEntity byId = sellerTemporaryService.getById(sellerId);
            if (byId == null) {
                // 新增
                seller.setCreateTime(new Date());
                sellerTemporaryService.save(seller);
            } else {
                // 修改
                seller.setSellerId(sellerId);
                sellerTemporaryService.updateById(seller);
            }
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 修改资料展示
     *
     * @return SellerEntity
     */
    @RequestMapping("/show")
    public Object show() {
        Long id = ShiroUtils.getUserId();
        SellerTemporaryEntity byId = sellerTemporaryService.getById(id);
        if (byId != null) {
            return byId;
        }
        return sellerService.getById(id);
    }

    /**
     * 获取实体-详情
     *
     * @return SellerTemporaryEntity
     */
    @RequestMapping("/findOne")
    public SellerTemporaryEntity findOne(String id) {
        return sellerTemporaryService.getById(id);
    }

    /**
     * 0暂存 1待审核 2审核通过 3审核未通过 4关闭
     * <p>
     * 2/3
     *
     * @return Result
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(String sellerId, String status) {
        try {
            log.info("temporary updateStatus sellerId[{}],status[{}]", sellerId, status);
            sellerTemporaryService.updateStatus(sellerId, status);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }
}
